/**
 * 
 */
package com.gopay.bis.ceb.ebank.operation;

import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ceb.ebank.CebB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;

/**
 * @CebB2cQueryHelper.java
 * @author Ryan
 * @2013-11-28 上午10:42:49 www.gopay.com.cn Inc.All rights reserved.
 */
public class CebB2cQueryHelper {
    private static final String separator = "\\~\\|\\~";
    
    private HttpTransport httpTransport;
    
    public Map<String,String> buildQueryParam(CebB2cMessage cebB2cMessage) throws GopayException{
        try {
            Map<String,String> param = new HashMap<String,String>();
            param.put("TransName", cebB2cMessage.getTransId());
            param.put("Plain", cebB2cMessage.getPlainMessage());
            param.put("Signature", cebB2cMessage.getSignMessage());
            param.put("url", cebB2cMessage.getBankUrl());
            return param;
        } catch (Exception e) {
            BankLogUtils.printB2CQueryExceptionLog(cebB2cMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6035);
        }
    }
    
    public Object query(Map<String,String> param)throws GopayException{
        BaseMessage baseMessage = new BaseMessage();
        baseMessage.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
        try {
            BankLogUtils.printB2CQuerySendLog(baseMessage, param);
            httpTransport.setUrl(param.get("url"));
            param.remove("url");
            String queryResult = (String)httpTransport.submit(param);
            BankLogUtils.printB2CQueryBlackLog(baseMessage,queryResult);
            return queryResult;
        } catch (Exception e) {
            BankLogUtils.printB2CQueryExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6037);
        }
    }

    
    public CebB2cMessage parseMessage(String queryResult) throws GopayException {
        BaseMessage baseMessage = new BaseMessage();
        baseMessage.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
        try {
            CebB2cMessage cebB2cMessage=null;
            if (queryResult != null) {
                
                String rspCodeStr = queryResult.substring(
                        queryResult.indexOf("ResponseCode=") + 13,
                        queryResult.indexOf("\r\n"));
                String plainStr = queryResult.substring(
                        queryResult.indexOf("Plain=") + 6,
                        queryResult.indexOf("Signature=") - 2);
                String signStr = queryResult.substring(
                        queryResult.indexOf("Signature=") + 10,
                        queryResult.lastIndexOf("\r\n"));
                
                cebB2cMessage= new CebB2cMessage();
                cebB2cMessage.setPlainMessage(plainStr);
                cebB2cMessage.setSignMessage(signStr);
                cebB2cMessage.setResponseCode(rspCodeStr);
                
                String[] reqParameters = cebB2cMessage.getPlainMessage().split(separator);
                 
                for (int i = 0; i < reqParameters.length; i++) {
                    String parameter = reqParameters[i];
                    String[] parameterArray = parameter.split("=");
                    if (parameterArray[0].equals("transId")) {
                    	cebB2cMessage.setTransId(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("originalTransId")) {
                    	cebB2cMessage.setOriginalTransId(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("orderId")) {
                    	cebB2cMessage.setOrderId(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("transSeqNo")) {
                    	cebB2cMessage.setTransSeqNo(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("merchantId")) {
                    	cebB2cMessage.setMerchantId(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("transDateTime")) {
                    	cebB2cMessage.setTransDateTime(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("ppDateTime")) {
                    	cebB2cMessage.setPpDateTime(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("clearingDate")) {
                    	cebB2cMessage.setClearingDate(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("transAmt")) {
                    	cebB2cMessage.setTransAmt(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("transAmt1")) {
                    	cebB2cMessage.setTransAmt1(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("feeAmt")) {
                    	cebB2cMessage.setFeeAmt(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("transStatus")) {
                    	cebB2cMessage.setTransStatus(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("respCode")) {
                    	cebB2cMessage.setRespCode(parameterArray[1]);
                        continue;
                    }
                    if (parameterArray[0].equals("payAcctType")) {
                    	cebB2cMessage.setPayAcctType(parameterArray[1]);
                        continue;
                    }
                }
            }
            return cebB2cMessage;
        } catch (Exception e) {
            BankLogUtils.printB2CQueryExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6039);
        }
    }

	public HttpTransport getHttpTransport() {
		return httpTransport;
	}

	public void setHttpTransport(HttpTransport httpTransport) {
		this.httpTransport = httpTransport;
	}

   

  
    
}
